package com.ys.store.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*定义拦截器*/
public class LoginInterceptor implements HandlerInterceptor {

    /*检测全局session对象中是否有uid数据，如果有则放行，如果没有则重定向页面*/
    /*返回值为true，则放行当前请求，false则拦截*/
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       /*若用户未曾登录，则重定向到登录页面*/
        if (request.getSession().getAttribute("uid") == null) {
            response.sendRedirect("/web/login.html");
            return false;
        }
        return true;
    }
}
